package com.bo.entity;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import com.bo.util.LoadResourceProxy;
import android.content.res.XmlResourceParser;
import android.graphics.Bitmap;
import android.util.Xml;


public class Product {
	
	private String id;
	private int model;
	private String title;
	private String content;
	private String time;
    private String companyId;
    private float rate;
	private List<String> imageList=new ArrayList<String>();
    private Bitmap bitmap;
   
	
    public Product(String companyId) {
		super();
		this.companyId = companyId;
	}


	public Product createProduct(String companyId,String id) throws Exception{
    	
    	Product p=new Product(companyId);
    	
		String xmlStr=LoadResourceProxy.getXMLString(LoadResourceProxy.PRODUCT,id);
		
		XmlPullParser xrp=Xml.newPullParser();

		if(xmlStr==null){
			
			throw new Exception("appear error");
		}
		
		StringReader reader=new StringReader(xmlStr);
		
		xrp.setInput(reader);
	    
	    boolean isProduct=false;
		
		while(xrp.getEventType()!=XmlResourceParser.END_DOCUMENT){
				
		  if(xrp.getEventType()==XmlResourceParser.START_TAG){
					
		       String tagName=xrp.getName();
		       
			   if(tagName.equalsIgnoreCase("product")){
						
				   isProduct=true;
				       
			   }else if(tagName.equalsIgnoreCase("id")&&isProduct){
				   
				   p.setId(xrp.nextText());
				   
			   }else if(tagName.equalsIgnoreCase("model")&&isProduct){
				   	
				   p.setModel(Integer.valueOf(xrp.nextText()));
				
			   }else if(tagName.equalsIgnoreCase("title")&&isProduct){
				   	
				   p.setTitle(xrp.nextText());
			   
			   }else if(tagName.equalsIgnoreCase("content")&&isProduct){
				   	
				   p.setContent(xrp.nextText());

			   }else if(tagName.equalsIgnoreCase("image")&&isProduct){
				   
				   p.getImageList().add(xrp.nextText());
			   
			   }
					
		   }else if(xrp.getEventType()==XmlResourceParser.END_TAG){
			   
			   String tagName=xrp.getName();
			   
			   if(tagName.equalsIgnoreCase("product")){
					
				   isProduct=false;    
				   
				   break;
			   }
		   }
				
			xrp.next();
		}
		
		return p;
    }
    

	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getModel() {
		return model;
	}
	public void setModel(int model) {
		this.model = model;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}

	public String getCompanyId() {
		return companyId;
	}

	public void setCompanyId(String companyId) {
		this.companyId = companyId;
	}

	public List<String> getImageList() {
		return imageList;
	}

	public void setImageList(List<String> imageList) {
		this.imageList = imageList;
	}


	public float getRate() {
		return rate;
	}


	public void setRate(float rate) {
		this.rate = rate;
	}


	public Bitmap getBitmap() {
		return bitmap;
	}


	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}

}
